From 53f499bc57c139a0a11f915f283565bf38e19ed3 Mon Sep 17 00:00:00 2001 From: "cl349@firebug.cl.cam.ac.uk" Date: Fri, 17 Jun 2005 17:22:24 +0000 Subject: [PATCH] bitkeeper revision 1.1713.3.9 (42b306d0pFeN318FP89TE8LRrhROMQ) xsobj.py: Cleanup interface to DB. Updated watches/event code. Signed-off-by: Mike Wray Signed-off-by: Christian Limpach --- tools/python/xen/xend/xenstore/xsobj.py | 77 +++++++++++++++---------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/tools/python/xen/xend/xenstore/xsobj.py b/tools/python/xen/xend/xenstore/xsobj.py index cdb35c1735..e16bf43e83 100644 --- a/tools/python/xen/xend/xenstore/xsobj.py +++ b/tools/python/xen/xend/xenstore/xsobj.py @@ -55,15 +55,15 @@ class DBConverter: getConverter = classmethod(getConverter) - def convertToDB(cls, val, ty=None): - return cls.getConverter(ty).toDB(val) + def exportTypeToDB(cls, db, path, val, ty=None): + return cls.getConverter(ty).exportToDB(db, path, val) - convertToDB = classmethod(convertToDB) + exportTypeToDB = classmethod(exportTypeToDB) - def convertFromDB(cls, data, ty=None): - return cls.getConverter(ty).fromDB(data.getData()) + def importTypeFromDB(cls, db, path, ty=None): + return cls.getConverter(ty).importFromDB(db, path) - convertFromDB = classmethod(convertFromDB) + importTypeFromDB = classmethod(importTypeFromDB) # Must define in subclass. name = None @@ -76,6 +76,26 @@ class DBConverter: raise ValueError("invalid converter name: '%s'" % self.name) self.converters[self.name] = self + def exportToDB(self, db, path, val): + if val is None: + return + try: + data = self.toDB(val) + except Exception, ex: + raise + setattr(db, path, data) + + def importFromDB(self, db, path): + data = getAttr(db, path) + if data is None: + val = None + else: + try: + val = self.fromDB(data.getData()) + except Exception, ex: + raise + return val + def toDB(self, val): raise NotImplementedError() @@ -185,10 +205,12 @@ class DBVar: self.attr = varpath[-1] def exportToDB(self, db, obj): - self.setDB(db, self.getObj(obj)) + val = self.getObj(obj) + DBConverter.exportTypeToDB(db, self.path, val, ty=self.ty) def importFromDB(self, db, obj): - self.setObj(obj, self.getDB(db)) + val = DBConverter.importTypeFromDB(db, self.path, ty=self.ty) + self.setObj(obj, val) def getObj(self, obj): o = obj @@ -207,21 +229,6 @@ class DBVar: return setAttr(o, self.attr, val) - def getDB(self, db): - data = getAttr(db, self.path) - if data is None: - return None - return DBConverter.convertFromDB(data, ty=self.ty) - - def setDB(self, db, val): - # Don't set in db if val is None. - #print 'DBVar>setDB>', self.path, 'val=', val - if val is None: - return - data = DBConverter.convertToDB(val, ty=self.ty) - #print 'DBVar>setDB>', self.path, 'data=', data - setAttr(db, self.path, data) - class DBMap(dict): """A persistent map. Extends dict with persistence. Set and get values using the usual map syntax: @@ -319,6 +326,21 @@ class DBMap(dict): traceback.print_exc() print 'DBMap>releaseDomain>', ex pass # todo: don't ignore + + def watch(self, fn, path=""): + return self.__db__.watch(fn, path=path) + + def unwatch(self, sid): + return self.__db__.unwatch(sid) + + def subscribe(self, event, fn): + return self.__db__.subscribe(event, fn) + + def unsubscribe(self, sid): + return self.__db__.unsubscribe(sid) + + def sendEvent(self, event, val): + return self.__db__.sendEvent(event, val) def transactionBegin(self): # Begin a transaction. @@ -333,12 +355,6 @@ class DBMap(dict): # We have changed values, what do we do? pass - def watch(self, fn): - pass - - def unwatch(self, watch): - pass - def checkName(self, k): if k == "": raise ValueError("invalid key, empty string") @@ -461,6 +477,9 @@ class DBMap(dict): n = n._addChild(x) return n + def getDB(self): + return self.__db__ + def setDB(self, db): if (db is not None) and not isinstance(db, XenNode): raise ValueError("invalid db") -- 2.30.2